<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body></body>
  <script>
    // 1.set去重
    let arr = [1, 2, 3, 4, 1, 5, 3, 2];
    // let arr1=[...new Set(arr)]
    // console.log(arr1)
    // 2. filter
    // let newArr = arr.filter((item, index, array) => {
    //   return array.indexOf(item) == index;
    // });
    // console.log(newArr);
    // 3.json去重
    // 思路:循环判断每一样  不相同就走else
    // let arr1 = {};
    // for (let i = 0; i < arr.length; i++) {
    //   let key = arr[i];
    //   if (arr1[key]) {
    //     arr1[key]++;
    //   } else {
    //     arr1[key] = 1;
    //   }
    // }
    // console.log(Object.keys(arr1));
    // 使用reduce 出现的次数
    let names=["a","b","c","a","e","a"]
    let json1=names.reduce((prev,next)=>{
        if(next in prev){
            prev[next]++
        }else {
            prev[next]=1
        }
        return prev
    },{})
    console.log(json1)
  </script>
</html>
